package com.gogowan.agroenterprise.widget.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gogowan.agroenterprise.R;
import com.gogowan.agroenterprise.model.CollectCode;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created by Administrator on 2018/6/20.
 */

public class NewCollectHistoryAdapater extends BaseAdapter {
    private LayoutInflater inflater=null;
    private Context context;
    private List<CollectCode> pList=new ArrayList<>();
    private List<Boolean> Checks=new ArrayList<>();
    private List<CheckBox> checkBoxes=new ArrayList<>();
    private onItemCheckBoxClickListener listener=null;
    public NewCollectHistoryAdapater(Context context, List<CollectCode> list){
        this.context=context;
        inflater=LayoutInflater.from(this.context);
        pList=list;
        Checks.clear();
        for (CollectCode s:pList){
            Checks.add(false);
        }
    }
    public void setpList(List<CollectCode> pList) {
        this.pList = pList;
        Checks.clear();
        for (CollectCode s:pList){
            Checks.add(false);
        }
    }
    public void setListener(onItemCheckBoxClickListener listener){
        this.listener=listener;
    }
    public List<CollectCode> isCheckItem(){
        List<CollectCode> cheks=new ArrayList<>();
        cheks.clear();
        for (int j=0;j<Checks.size();j++){
            if (Checks.get(j)){
                cheks.add(pList.get(j));
            }
        }
        return  cheks;
    }
    //全选 或者 全部不选 调用
    public void AllCheck(boolean b){
        for (CheckBox box:checkBoxes){
            if (box.isChecked()==b){
                box.performClick();
            }
        }
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return pList.size();
    }

    @Override
    public Object getItem(int position) {
        return pList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Houlder houlder=null;
        if (convertView==null){
            convertView=inflater.inflate(R.layout.layout_newhistroy_item,null);
            houlder=new Houlder();
            houlder.isCheckBox=(CheckBox)convertView.findViewById(R.id.history_check);
            houlder.No_Tv=(TextView)convertView.findViewById(R.id.xh);
            houlder.Shop_OrderTime_Tv=(TextView)convertView.findViewById(R.id.jdsj);
            houlder.Shop_OrderR_Tv=(TextView)convertView.findViewById(R.id.jdr);
            houlder.CodeNum_Tv=(TextView)convertView.findViewById(R.id.bqnum);
            houlder.ll=(LinearLayout)convertView.findViewById(R.id.item_ll);
            checkBoxes.add(houlder.isCheckBox);
            convertView.setTag(houlder);
        }else {
            houlder=(Houlder)convertView.getTag();
        }
        houlder.isCheckBox.setChecked(Checks.get(position));
        final Houlder finalHoulder = houlder;
        houlder.ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finalHoulder.isCheckBox.performClick();
            }
        });
        houlder.isCheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Checks.set(position,finalHoulder.isCheckBox.isChecked());
                if (listener!=null){
                    listener.onClick();
                }
            }
        });
        houlder.No_Tv.setText((position+1)+"");
        houlder.Shop_OrderTime_Tv.setText(pList.get(position).getCollectTime());
        houlder.Shop_OrderR_Tv.setText(pList.get(position).getCollectUserName());
        houlder.CodeNum_Tv.setText(pList.get(position).getBqsum()+"");
        return convertView;
    }
    class Houlder{
        CheckBox isCheckBox;
        TextView No_Tv,Shop_OrderTime_Tv,Shop_OrderR_Tv,CodeNum_Tv;
        LinearLayout ll;
    }
    public interface  onItemCheckBoxClickListener{
        void onClick();
    }
}
